﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TFSLibrary.cs" company="">
//   
// </copyright>
// <summary>
//   Facade class for TFS API calls
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UITest.Helpers.TFS
{
    using System;
    using System.Net;

    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.TestManagement.Client;

    /// <summary>
    ///     Facade class for TFS API calls
    /// </summary>
    public class TFSLibrary
    {
        #region Constants

        private const string TfsProject = "xxx";
        private const string TfsUri = "http:\\xxxxxxx";

        private const string VsoPatUser = "xxx";
        private const string VsoPat = "xxxxxxxxxx";

        #endregion

        #region Fields

        private readonly string project;

        private readonly string tfsUri;

        #endregion

        #region Constructors and Destructors

        private TFSLibrary(string tfsUri, string project)
        {
            this.tfsUri = tfsUri;
            this.project = project;
            this.Tfs = new TfsTeamProjectCollection(new Uri(this.tfsUri), this.TfsClient());
            this.Tfs.Authenticate();
            this.TestRun = new TestRunModule(this);
            this.TestPlan = new TestPlanModule(this);
            this.TestCase = new TestCaseModule(this);
            this.TestSuite = new TestSuiteModule(this);
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the test case.
        /// </summary>
        public TestCaseModule TestCase { get; private set; }

        /// <summary>
        /// Gets the test management.
        /// </summary>
        public ITestManagementTeamProject TestManagement
        {
            get
            {
                var testManagement = (ITestManagementService)this.Tfs.GetService(typeof(ITestManagementService));

                return testManagement.GetTeamProject(this.project);
            }
        }

        /// <summary>
        /// Gets the test plan.
        /// </summary>
        public TestPlanModule TestPlan { get; private set; }

        /// <summary>
        /// Gets the test run.
        /// </summary>
        public TestRunModule TestRun { get; private set; }

        /// <summary>
        /// Gets the test suite.
        /// </summary>
        public TestSuiteModule TestSuite { get; private set; }

        /// <summary>
        /// </summary>
        public TfsTeamProjectCollection Tfs { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Opens TFS TeamProjectCollection
        /// </summary>
        /// <param name="tfsUri">
        /// </param>
        /// <param name="project">
        /// </param>
        /// <returns>
        /// The <see cref="TFSLibrary"/>.
        /// </returns>
        public static TFSLibrary Open(string tfsUri = TfsUri, string project = TfsProject)
        {
            var lib = new TFSLibrary(tfsUri, project);
            return lib;
        }

        #endregion

        #region Methods

        private TfsClientCredentials TfsClient()
        {
            var networkCredential = new NetworkCredential(VsoPatUser, VsoPat);
            var basicCredential = new BasicAuthCredential(networkCredential);

            var tfsCredentials = new TfsClientCredentials(basicCredential) { AllowInteractive = false };
            return tfsCredentials;
        }

        #endregion
    }
}